﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using System.Data;
using cbmobile.BLC;
using System.Data.SqlClient;

namespace cbmobile.BLC
{
    public class BLC_LoaiLinhKien
    {
        private CBMobileEntities da = null;
        public BLC_LoaiLinhKien()
        {
            da = new CBMobileEntities();
        }


        public IList<LoaiLinhKien> All_LoaiLinhKien()
        {
            return da.LoaiLinhKiens.ToList();
        }


        public LoaiLinhKien Get_LoaiLinhKien_By_ID(int Ma_LoaiLinhKien)
        {
            IList<LoaiLinhKien> list = da.LoaiLinhKiens.Where(z => z.Ma_LoaiLinhKien == Ma_LoaiLinhKien).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LoaiLinhKien> Row_LoaiLinhKien(string p_name)
        {

            IList<LoaiLinhKien> list = da.LoaiLinhKiens.Where(z => z.Ten_LoaiLinhKien.Contains(p_name)).ToList();
            return list;
        }

        public LoaiLinhKien Get_PK_By_NameLK(string TenLoai_PK)
        {
            IList<LoaiLinhKien> list = da.LoaiLinhKiens.Where(z => z.Ten_LoaiLinhKien.Contains(TenLoai_PK)).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LoaiLinhKien> Get_PK_By_IDMaPK(int MaPK)
        {
            IList<LoaiLinhKien> list = da.LoaiLinhKiens.Where(z => z.Ma_LoaiLinhKien == MaPK).ToList();
            if (list.Count() == 0)
                return null;
            return list;
        }

        public int GetTotalRecord(string p_name)
        {
            return da.LoaiLinhKiens.Where(z => z.Ten_LoaiLinhKien.Contains(p_name)).Count();
        }

        public void Delete(int Ma_LoaiLinhKien)
        {
            var existing = da.LoaiLinhKiens.Where(p => p.Ma_LoaiLinhKien == Ma_LoaiLinhKien).First();
            existing.TrangThai = (int)CommonStatus.Delete;
            da.SaveChanges();
        }


        #region create
        public void Create(string Ten_LoaiLinhKien, int ThuTu, int TrangThai)
        {
            var objEnt = new LoaiLinhKien();
            objEnt.Ten_LoaiLinhKien = Ten_LoaiLinhKien;
            objEnt.TrangThai = TrangThai;
            objEnt.ThuTu = ThuTu;

            da.LoaiLinhKiens.AddObject(objEnt);
            da.SaveChanges();
        }
        #endregion

        #region update
        public void Update(int Ma_LoaiLinhKien, string Ten_LoaiLinhKien, int ThuTu, int TrangThai)
        {
            var role = new LoaiLinhKien();
            var objEnt = da.LoaiLinhKiens.Where(p => p.Ma_LoaiLinhKien == Ma_LoaiLinhKien).First();
            objEnt.Ten_LoaiLinhKien = Ten_LoaiLinhKien;
            objEnt.TrangThai = TrangThai;
            objEnt.ThuTu = ThuTu;

            da.SaveChanges();
        }
        #endregion

        public void Create(LoaiLinhKien role)
        {
            da.LoaiLinhKiens.AddObject(role);
            da.SaveChanges();
        }
    }
}